/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.fatecpg.songs.intranet.managedbeans;

import br.com.fatecpg.songs.crud.facade.RegisterFacade;
import br.com.fatecpg.songs.dao.util.DAOException;
import br.com.fatecpg.songs.entity.Artist;
import br.com.fatecpg.songs.entity.Musician;
import br.com.fatecpg.songs.util.FacesContextUtil;
import java.util.ArrayList;

import java.util.List;
import javax.faces.bean.ManagedBean;

import javax.faces.bean.ViewScoped;
import org.hibernate.exception.ConstraintViolationException;

/**
 *
 * @author Marcos
 */
@ManagedBean
@ViewScoped
public class ArtistManagedBean implements RegisterManagedBean<Artist>{        
    private int viewState;    
    private RegisterFacade<Artist> facade = new RegisterFacade<Artist>(Artist.class);
    private Artist artist;         
    private Long[] musiciansId;
    private List<Artist> list;
    
    public ArtistManagedBean() throws DAOException {        
        artist = new Artist();          
        list = facade.list();
        viewState = LIST_STATE;
    }    
                      
    public void setArtist(Artist artist) {
        this.artist = artist;
    }       
    
    @Override
    public void openNew() {
        artist = new Artist();
        musiciansId = null;
        viewState = REGISTER_STATE;
    }

    @Override
    public void openToUpdate() throws DAOException {        
        this.artist = facade.load();
        setMusicians(artist.getMusicians());
        this.viewState = REGISTER_STATE;
    }

    @Override
    public void register() throws DAOException {
        artist.setMusicians(getMusicians());
        facade.setUpdateInfo(artist);        
        facade.register(artist);
        list = facade.list();  
        viewState = LIST_STATE;        
    }
    
    public List<Musician> getMusicians(){
        List<Musician> musicians = new ArrayList<Musician>();
        for(int i = 0; i < musiciansId.length; i++){
            Musician musician = new Musician();
            musician.setId(musiciansId[i]);
            musicians.add(musician);
        }
        return musicians;
    }

    private void setMusicians(List<Musician> musicians){
        musiciansId = new Long[musicians.size()];
        for(int i = 0; i < musicians.size(); i++){
            musiciansId[i] = musicians.get(i).getId();
        }        
    }
      
    @Override
    public void delete() throws DAOException { 
        try{
            facade.delete();
            list = facade.list();
            viewState = LIST_STATE;            
        }catch(ConstraintViolationException ex){
            FacesContextUtil.addMessage("Existem albuns deste artista. Para deletá-lo, é necessário deletar os albums.");            
        }
    }
 
    @Override
    public void cancel() {
        this.artist = new Artist();
        this.viewState = LIST_STATE;        
    }    
    
    public int getViewState() {
        return viewState;
    }

    public void setViewState(int viewState) {
        this.viewState = viewState;
    }

    @Override
    public List<Artist> getList(){
        return list;
    }
    
    public Artist getArtist() {
        return artist;
    }

    public Long[] getMusiciansId() {
        return musiciansId;
    }

    public void setMusiciansId(Long[] musiciansId) {
        this.musiciansId = musiciansId;
    }     
}
